//
//  MPTAuthorizationContactsTools.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/6/26.
//  Copyright © 2017年 Jeakin. All rights reserved.
//
//


#import "MPTAuthorizationContactsTools.h"
#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h>


@interface MPTAuthorizationContactsTools ()
{
    AuthorizationHandler _completion;
}


@end


@implementation MPTAuthorizationContactsTools

/**
 注释

 @return 注释
 */
+ (instancetype)sharedInstance
{
    static MPTAuthorizationContactsTools *_instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _instance = [[MPTAuthorizationContactsTools alloc] init];
    });
    
    return _instance;
}


#pragma mark - Microphone

- (MPE_AuthorizationStatus)authorizationStatus
{
    if ([[UIDevice currentDevice].systemVersion floatValue] < 9.0)
    {
        ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
        switch (ABstatus)
        {
            case kABAuthorizationStatusAuthorized:
            {
                return MPE_AuthorizationAuthorized;
            }
                break;
            case kABAuthorizationStatusDenied:
            case kABAuthorizationStatusRestricted:
            {
                return MPE_AuthorizationDenied;
            }
                break;
            case kABAuthorizationStatusNotDetermined:
            {
                return MPE_AuthorizationNotDetermined;
            }
                break;
            default:
                return MPE_AuthorizationAuthorized;
                break;
        }
    }
    else
    {
        CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
        switch (status)
        {
            case CNAuthorizationStatusAuthorized:
            {
                return MPE_AuthorizationAuthorized;
            }
                break;
            case CNAuthorizationStatusDenied:
            case CNAuthorizationStatusRestricted:
            {
                return MPE_AuthorizationDenied;
            }
                break;
            case CNAuthorizationStatusNotDetermined:
            {
                return MPE_AuthorizationNotDetermined;
            }
                break;
                
            default:
                return MPE_AuthorizationAuthorized;
                break;
        }
    }
}

- (void)authorize:(AuthorizationHandler)completion
{
    MPE_AuthorizationStatus status = [self authorizationStatus];
    switch (status)
    {
        case MPE_AuthorizationAuthorized:
        case MPE_AuthorizationDenied:
        {
            if (completion)
            {
                completion(status, nil);
            }
        }
            break;
        case MPE_AuthorizationNotDetermined:
        {
            if (completion)
            {
                _completion = completion;
            }
            
            [self actuallyAuthorize];
        }
            break;
            
        default:
        {
            if (completion)
            {
                completion(MPE_AuthorizationAuthorized, nil);
            }
        }
            break;
    }
}

- (MPE_AuthorizationType)permissionType
{
    return MPE_AuthorizationContacts;
}

- (void)actuallyAuthorize
{
    
    MPE_AuthorizationStatus status = [self authorizationStatus];
    switch (status)
    {
        case MPE_AuthorizationAuthorized:
        case MPE_AuthorizationDenied:
        {
            if (_completion)
            {
                _completion(status, nil);
            }
        }
            break;
        case MPE_AuthorizationNotDetermined:
        {
            if ([[UIDevice currentDevice].systemVersion floatValue] < 9.0)
            {
                ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
                ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                    
                    if (_completion)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^
                        {
                            if (granted)
                            {
                                _completion(MPE_AuthorizationAuthorized, nil);
                            }
                            else
                            {
                                _completion(MPE_AuthorizationDenied, nil);
                            }
                        });
                    }
                });
            }
            else
            {
                CNContactStore *contactStore = [[CNContactStore alloc] init];
                [contactStore requestAccessForEntityType:CNEntityTypeContacts
                                       completionHandler:^(BOOL granted, NSError * _Nullable error)
                {
                    if (_completion)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^
                        {
                            if (granted)
                            {
                                _completion(MPE_AuthorizationAuthorized, nil);
                            }
                            else
                            {
                                _completion(MPE_AuthorizationDenied, error);
                            }
                        });
                    }
                }];
            }
        }
            break;
            
        default:
        {
            if (_completion)
            {
                _completion(MPE_AuthorizationAuthorized, nil);
            }
        }
            break;
    }
}


@end
